﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lion.WeiXin.Core.Data;
using Lion.WeiXin.Core.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Lion.WeiXin.Controllers
{
    [PermissionFilter]
    public class CommandController : Controller
    {
        ICommand icmd = new CommandDAO();

        /*jquery 1.4 以后的版本，返回的json 字符串，必须都要有双引号，例如
         * {"myvalue":1}
{"myvalue":"red"}
{"myvalue":["black",250]}
         */

        public ActionResult CmdList()
        {
            return View();
        }
        /// <summary>
        ///  获取分页列表数据
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="msgType"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public string GetPageList(string keyword, string msgType, int currentPage, int pageSize)
        {
            List<CommandModel> list = icmd.GetAllCommand();
            int totalCount = list.Count;
            list = list.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();

            //return Json(new { total = totalCount, data = list });

            string jsonResult = JsonConvert.SerializeObject(list, new IsoDateTimeConverter());
            jsonResult = "{\"total\":" + totalCount + "," + "\"data\":" + jsonResult + "}";
            return jsonResult;
        }

        public ActionResult Show()
        {
            string action = Request.QueryString["action"];
            string ID = Request.QueryString["ID"];
            CommandModel model = new CommandModel();
            if (action == ActionEnum.Update.ToString() || action == ActionEnum.Details.ToString())
            {
                model = icmd.GetAllCommand().Where(n => n.AID == int.Parse(ID)).FirstOrDefault();
                if (model == null)
                {
                    model = new CommandModel();
                }
            }

            //下拉列表框
            List<SelectListItem> items = new List<SelectListItem>();
            SelectListItem item = new SelectListItem() { Value = "0", Text = "请选择" };
            SelectListItem item2= new SelectListItem(){ Value = "text",Text = "文本"};
            SelectListItem item3 = new SelectListItem() { Value = "image", Text = "图片" };
            items.Add(item);
            items.Add(item2);
            items.Add(item3);
            item3.Selected = true;
           // ViewBag.MsgTypeSelect = items;
            ViewData["MsgTypeSelect"] = items;

            return View(model);
        }

        public ActionResult Create()
        { 
            CommandModel model = new CommandModel();
            icmd.Creaet(model);
            return View();
        }


        public ActionResult Save(FormCollection form)
        {
            string action = Request.QueryString["action"];
            int proId =int.Parse( form["hdProductId"]);
            string code = form["txtCode"];
            string password = form["txtPassword"];
            decimal fee = decimal.Parse( form["txtFee"]);
            string phone = form["txtPhone"];
            string address = form["txtAddress"];
             
            //检查卡号和密码是否正确
            int reCode = 0;  // 0 表示成功，大于0 的都表示失败
            IVipCard ivip = new VipCardDAO();
            VipCardModel card = ivip.GetList(string.Format("Code='{0}'", code))[0];
            if (card.Password != password)
            {
                reCode = 10;  //卡号或密码错误
            }

            if (card.Balance <= fee)
            {
                reCode = 11;  // 卡号余额不足
            }

            // 保存订单，
            // 订单详细
            // 购买记录

            return Json(new { Code=reCode});
        }

    }
}
